<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="定时任务配置" @del="del" @newCreate="newCreate" @requestFieldConfig="requestFieldConfig"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
      @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'定时任务配置'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form" @handleInput="handleInput"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'timeTaskConfig',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null,
        jobName: '',
        jobDescr: '',
        targetUrl: '',
        resultTemplate: ''
      },
      options: {
        jobDetails: []
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '任务名',
          prop: 'jobName',
          input: 'input'
        },
        {
          label: '任务描述',
          prop: 'jobDescr',
          input: 'input'
        },
        {
          label: '仓库类型',
          prop: 'warehouseType',
          input: 'select',
          options: [
            {"label":"激活的仓库","value":"1"},
            {"label":"激活的仓库（不包含企业级）","value":"2"},
            {"label":"指定仓库","value":"3"},
            {"label":"自定义仓库","value":"4"}
          ]
        },
        {
          label: '目标地址',
          prop: 'targetUrl',
        },
        {
          label: '返回模板',
          prop: 'resultTemplate',
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '任务名',
          prop: 'jobName',
          type: 'select',
          options: this.options.jobDetails,
          disabled: this.dialogModule.type === 'edit'
        },
        {
          label: '任务描述',
          prop: 'jobDescr',
          type: 'input',
          disabled: true
        },
        {
          label: '仓库类型',
          prop: 'warehouseType',
          type: 'select',//1激活的仓库，2激活的仓库（不包含企业级），3指定仓库，4自定义仓库
          options: [
              {"label":"激活的仓库","value":"1"},
              {"label":"激活的仓库（不包含企业级）","value":"2"},
              {"label":"指定仓库","value":"3"},
              {"label":"自定义仓库","value":"4"}
          ]
        },
        {
          label: '仓库',
          prop: 'warehouse',
          type: 'input'
        },
        {
          label: '目标地址',
          prop: 'targetUrl',
          type: 'input'
        },
        {
          label: '返回模板',
          prop: 'resultTemplate',
          type: 'input'
        }
      ]
    },
    formRules() {
      return {
        jobName: [
          {
            required: true,
            message: '任务名',
            trigger: 'change'
          }
        ]
      }
    },
    apiMap() {
      return {
        getListApi: '/v1/ediJobConfig/selectEdiJobConfigList',
        getInfoApi: '/v1/ediJobConfig/selectEdiJobConfigById',
        deleteApi: '/v1/ediJobConfig/deleteEdiJobConfigByIds',
        editApi: '/v1/ediJobConfig/editEdiJobConfig',
        addApi: '/v1/ediJobConfig/addEdiJobConfig'
      }
    }
  },
  methods: {
    requestFieldConfig(data){
      console.log(data);
      if(data.length != 1){
        return this.$message.error("请选择一条数据进行操作")
      }
      if(data[0].mainclass == ''){
        return this.$message.error("请选择wms主表");
      }
      this.$router.push({ path: '/apiBase/tableConfig', query: { id: data[0].id } })
    },
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail?.resetFields()
          this.dialogModule.flag = false
        })
      })
    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.jobDescr}]定时任务配置`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增定时任务配置'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },
    getOptions() {
      this.$post('/v1/job/selectJobList',{
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        const dataList = res.data.page.records;
        this.options.jobDetails = dataList.map(i => {
          i.label = i.description
          i.value = i.jobName
          return i
        })
      });
    },
    handleInput(prop, val, obj) {
      if(prop === 'jobName'){
        this.form.jobDescr = obj.description
      }
    },
    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
    this.getOptions()
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
